import gradio as gr
import cv2
import numpy as np
import pickle
import faiss

with open('data/best_model.pkl', 'rb') as file:  # 替换为您的模型文件路径
    best_clf = pickle.load(file)

# Gradio界面函数
def classify_image(image):
    img = cv2.imdecode(np.fromstring(image, np.uint8), cv2.IMREAD_COLOR)
    img = cv2.resize(image, (224, 224))
    img = img.reshape(1, -1).astype('float32')
    faiss.normalize_L2(img)
    
    prediction = best_clf.predict(img)
    return "猫" if prediction[0] == 0 else "狗"

# 创建Gradio界面
iface = gr.Interface(fn=classify_image, inputs="image", outputs="text")
iface.launch()